﻿using KAsFrame.Application.Contracts.Certificate;
using KAsFrame.Common.Application;
using KAsFrame.Common.Dto;
using KAsFrame.Common.Extensions;
using KAsFrame.Domain.Certificate.Dto;
using KAsFrame.Domain.Certificate.Entities;
using KAsFrame.Domain.User.Entities;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KAsFrame.Application.AppService.Certificate
{
    public class TemplateAppService : CommonCurdAppService<long, Template, TemplateCreateDto, TemplateCreateDto, TemplateDto>, ITemplateAppService
    {
        public TemplateAppService(ISqlSugarClient db) : base(db)
        {
        }

        public override Task<long> CreateAsync(TemplateCreateDto createDto)
        {
            if (!_db.Queryable<UserInfo>().Any(x => x.Id == createDto.Uid))
                throw new Exception("所选用户不存在");
            foreach (var item in createDto.LabelList)
                if (item.Length > 8)
                    throw new Exception("内容标题不能大于8个字符");

            createDto.Title = createDto.Title.IsNullOrWhiteSpace()?DateTime.Now.ToString("yyyyMMddhhmmss") : createDto.Title;
            createDto.BackgroundColor = createDto.BackgroundColor.IsNullOrWhiteSpace()? "rgb(161, 53, 39, 0.7)": createDto.BackgroundColor;

            return base.CreateAsync(createDto);
        }

        public override async Task<PageReturnDto<TemplateDto>> GetPageListAsync(PageReqDto pageReqDto)
        {
            Expressionable<Template> exp = Expressionable.Create<Template>();
            if (pageReqDto.Condition.Count > 0)
            {
                var condition = pageReqDto.Condition;

                //var nameKey = condition.Keys.FirstOrDefault(x => x.Contains("name", StringComparison.OrdinalIgnoreCase));
                //if (nameKey != null)
                //    exp = exp.And(x => x.Name.Contains(condition[nameKey].ToString()));
            }

            return await PageAsync(pageReqDto.PageIndex, pageReqDto.PageSize, exp, pageReqDto.OrderBy);
        }
    }
}
